﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Babylon.Toolbox
{
    public class VertexColorEffect : ModelEffect
    {
        readonly EffectParameter worldViewProjectionParameter;
        readonly EffectParameter worldParameter;
        readonly EffectParameter booleans01Parameter;
        readonly EffectParameter diffuseColorParameter;
        readonly EffectParameter specularColorParameter;
        readonly EffectParameter emissiveColorParameter;
        readonly EffectParameter ambientColorParameter;
        readonly EffectParameter lightPositionParameter;
        readonly EffectParameter cameraPositionParameter;


        public VertexColorEffect(GraphicsDevice device)
            : this(device, "Babylon.Toolbox", "Effects/VertexColorEffect/VertexColorEffect")
        {
           
        }

        protected VertexColorEffect(GraphicsDevice device, string assemblyName, string rootName)
            : base(device, assemblyName, rootName)
        {
            worldViewProjectionParameter = GetParameter("WorldViewProjection");
            worldParameter = GetParameter("World");
            diffuseColorParameter = GetParameter("DiffuseColor");
            emissiveColorParameter = GetParameter("EmissiveColor");
            specularColorParameter = GetParameter("SpecularColor");
            ambientColorParameter = GetParameter("AmbientColor");
            booleans01Parameter = GetParameter("Booleans01");
            lightPositionParameter = GetParameter("LightPosition");
            cameraPositionParameter = GetParameter("CameraPosition");

           

            Alpha = 1.0f;
            SpecularColor = Color.Black;
            SpecularPower = 16.0f;
        }

        public string Name { get; set; }

        public override Matrix World { get; set; }
        public override Matrix View { get; set; }
        public override Matrix Projection { get; set; }

        public Color DiffuseColor { get; set; }

        public Color AmbientColor { get; set; }
        public Color SceneAmbientColor { get; set; }

        public Color EmissiveColor { get; set; }

        public Color SpecularColor { get; set; }
        public float SpecularPower { get; set; }


        public Vector3 LightPosition { get; set; }
        public Vector3 CameraPosition { get; set; }

        public float Alpha { get; set; }

        public override void Apply()
        {
            Vector4 booleans01 = Vector4.Zero;
            
            worldViewProjectionParameter.SetValue(World * View * Projection);
            worldParameter.SetValue(World);
            
            emissiveColorParameter.SetValue(EmissiveColor);
            specularColorParameter.SetValue(SpecularColor, SpecularPower);
            ambientColorParameter.SetValue(AmbientColor.Multiply(SceneAmbientColor));

            lightPositionParameter.SetValue(LightPosition);
            cameraPositionParameter.SetValue(CameraPosition);

            
                diffuseColorParameter.SetValue(DiffuseColor, Alpha);
            



            base.Apply();
        }

        public override string ToString()
        {
            return Name;
        }

    }
}
